home *** CD-ROM | disk | FTP | other *** search
/ NeXT Education Software Sampler 1992 Fall / NeXT Education Software Sampler 1992 Fall.iso / Programming / Classes / CryptSums / cryptsums.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-05-18  |  2.2 KB  |  78 lines

  1. /* cryptsums.c -- copyright 1992 by C.D.Lane */
  2.  
  3. #include <c.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <stdarg.h>
  7. #include <strings.h>
  8.  
  9. #include <sys/loader.h>
  10.  
  11. #include "cryptsum.h"
  12.  
  13. typedef enum {PROGRAM, FILENAME, PASSWORD, ARGC} ARGUMENTS;
  14.  
  15. void error_puts(char *string)
  16. {
  17.     perror(string);
  18.     exit(EXIT_FAILURE);
  19. }
  20.  
  21. void error_printf(const char *format, ...)
  22. {
  23.     va_list ap;
  24.     
  25.     va_start(ap, format); {
  26.             (void) vfprintf(stderr, format, ap);
  27.         } va_end(ap);
  28.  
  29.     exit(EXIT_FAILURE);
  30. }
  31.  
  32. void main(int argc, char *argv[])
  33. {
  34.     FILE *stream;
  35.     char *password;
  36.     unsigned short halfword;
  37.     long l_offset, s_offset;
  38.     unsigned long ncmds, nsects, size;
  39.     
  40.     struct section s_section;
  41.     struct mach_header m_header;
  42.     struct load_command l_command;
  43.     struct segment_command s_command;
  44.     
  45.     if (argc != ARGC) error_printf("Usage:  %s Application Password\n", argv[PROGRAM]);
  46.     
  47.     if ((stream = fopen(argv[FILENAME], "r")) == NULL) error_puts("fopen");
  48.     if (fread(&m_header, sizeof(m_header), 1, stream) == 0) error_puts("fread");
  49.     if (m_header.magic != MH_MAGIC) error_printf("Incorrect Mach magic number %x!\n", m_header.magic);
  50.  
  51.     for (ncmds = m_header.ncmds; ncmds > 0; ncmds--) {
  52.         l_offset = ftell(stream);
  53.         if (fread(&l_command, sizeof(l_command), 1, stream) == 0) error_puts("fread");
  54.         if (l_command.cmd == LC_SEGMENT) {        
  55.             if (fseek(stream, l_offset, SEEK_SET) == CERROR) error_puts("fseek");
  56.             if (fread(&s_command, sizeof(s_command), 1, stream) == 0) error_puts("fread");
  57.             if (strcmp(s_command.segname, CS_SEGMENT) != 0) {
  58.                 for (nsects = s_command.nsects; nsects > 0; nsects--) {
  59.                     if (fread(&s_section, sizeof(s_section), 1, stream) == 0) error_puts("fread");
  60.                     s_offset = ftell(stream);
  61.                     if (fseek(stream, s_section.offset, SEEK_SET) == CERROR) error_puts("fseek");
  62.  
  63.                     for (size = 0L, halfword = 0; size < s_section.size; size++)
  64.                         halfword = cs_checksum(halfword, getc(stream));
  65.                     
  66.                     password = cs_cryptkey(argv[PASSWORD], halfword);
  67.  
  68.                     (void) printf("%s %s %s\n", s_command.segname, s_section.sectname, password);
  69.  
  70.                     if (fseek(stream, s_offset, SEEK_SET) == CERROR) error_puts("fseek");
  71.                     }
  72.                 }
  73.             }
  74.         if(fseek(stream, l_command.cmdsize + l_offset, SEEK_SET) == CERROR) error_puts("fseek");
  75.         }
  76.     exit(EXIT_SUCCESS);
  77. }
  78.